package com.gizwits.opensource.appkit.yasystem;

import android.app.Activity;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.gizwits.gizwifisdk.api.GizWifiDevice;
import com.gizwits.gizwifisdk.enumration.GizWifiErrorCode;
import com.gizwits.gizwifisdk.listener.GizWifiDeviceListener;
import com.gizwits.opensource.appkit.ControlModule.GosControlModuleBaseActivity;
import com.gizwits.opensource.appkit.R;

import java.util.concurrent.ConcurrentHashMap;

public class Secend1Activity extends GosControlModuleBaseActivity {
    Button button1,button2,button3,button4;
    private LocalBroadcastManager localBroadcastManager;
    String data;
    String Stone="";
    int color=0;
    String tempdata;
    TextView textView;
    private GizWifiDevice device;
    GosControlModuleBaseActivity go;
    ImageView imageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_secend1);
        go = new GosControlModuleBaseActivity();
        localBroadcastManager = LocalBroadcastManager.getInstance(this);
        button1 = (Button) findViewById(R.id.btn2);
        textView = (TextView) findViewById(R.id.tv1);
        Intent intent = getIntent();
        if(intent!=null) {
            Stone = intent.getStringExtra("Stone");
            device = (GizWifiDevice) intent.getParcelableExtra("GizWifiDevice");

            Log.d("汪涛123", device.getProductName());
            device.setListener(mGizWifiDeviceListener);
        }
        color= intent.getIntExtra("color",0);
        button2 = (Button) findViewById(R.id.btn3);
        button3 = (Button) findViewById(R.id.btn4);
        button4 = (Button) findViewById(R.id.btn5);
        imageView = (ImageView) findViewById(R.id.image);
        button1.setOnClickListener(onClickListener);
        button2.setOnClickListener(onClickListener);
        button3.setOnClickListener(onClickListener);
        button4.setOnClickListener(onClickListener);
        imageView.setOnClickListener(onClickListener);
        if (Stone.equals("true")){
            color(1);
        }
    }
    Button.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.btn2:
                    color=1;
                    data = "SyaustartE";
                    retun(data);
                    color(color);
                    break;
                case R.id.btn3:
                    color=2;
                    data="SyaustopE";
                    retun(data);
                    color(color);
                    break;
                case R.id.btn4:
                    data="SyaurestE";
                    retun(data);
                    break;
                case R.id.btn5:
                    data = "SycloseautoE";
                    retun(data);
                    finish();
                    break;
                case R.id.image:
                    data = "SycloseautoE";
                    retun(data);
                    finish();
                    break;
                default:
                    break;
            }
        }
    };


    @Override
    protected void onDestroy() {
        data = "ScloseautoE";
        retun(data);;
        super.onDestroy();
    }
    private  void color(int i){
        switch (i){
            case 1:
                button1.setBackgroundColor(Color.parseColor("#FF9500"));
                button2.setBackgroundColor(Color.parseColor("#F8DC26"));
                break;
            case 2:
                button1.setBackgroundColor(Color.parseColor("#F8DC26"));
                button2.setBackgroundColor(Color.parseColor("#FF9500"));
                break;
                default:
                    break;
        }
    }
    public void retun(String data){
        Intent intent = new Intent("com.example.broadcasttest.LOCAL_BROADCAST");
        intent.putExtra("key",data);
        intent.putExtra("data",true);
        localBroadcastManager.sendBroadcast(intent);
    }

    protected void updateUI() {
        tempdata = "";
        if (data_fire == true){
            show("有火灾发生！！");
        }
        tempdata+="剩余压面时间"+data_yatime+"  ";
        textView.setText(tempdata);
        if (data_yatime==0){
            show("压面完成");
        }
    }

    ///显示消息
    public void show(String message){
        Bitmap btm = BitmapFactory.decodeResource(getResources(),
                R.drawable.back);
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                Secend1Activity.this).setSmallIcon(R.drawable.back)
                .setContentTitle("压面机系统")
                .setContentText(message);
        mBuilder.setTicker("您有一条新消息");//第一次提示消息的时候显示在通知栏上
        mBuilder.setNumber(12);
        mBuilder.setLargeIcon(btm);
        mBuilder.setAutoCancel(true);//自己维护通知的消失
//获取通知管理器对象
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(0, mBuilder.build());
    }
    private GizWifiDeviceListener mGizWifiDeviceListener= new GizWifiDeviceListener(){

        @Override
        public void didReceiveData(GizWifiErrorCode result, GizWifiDevice device, ConcurrentHashMap<String, Object> dataMap, int sn) {
            super.didReceiveData(result, device, dataMap, sn);
            Log.d("接收到的数据",dataMap.toString());
            go.getDataFromReceiveDataMap(dataMap);
            updateUI();
        }
    };
}

